#include "page_adapter_up.h"
#include "wind_mgr.h"
#include "cdlog.h"
#include "page_base.h"
#include "R.h"

MyPageAdapterUp::MyPageAdapterUp()
{

    dataList.emplace_back(0, nullptr);
    dataList.emplace_back(1, nullptr);
    dataList.emplace_back(2, nullptr);
    dataList.emplace_back(3, nullptr);
    dataList.emplace_back(4, nullptr);
    dataList.emplace_back(5, nullptr);
    dataList.emplace_back(6, nullptr);
    dataList.emplace_back(7, nullptr);
    dataList.emplace_back(8, nullptr);
    dataList.emplace_back(9, nullptr);
    dataList.emplace_back(10, nullptr);
    dataList.emplace_back(11, nullptr);
    dataList.emplace_back(12, nullptr);
    dataList.emplace_back(13, nullptr);
    dataList.emplace_back(14, nullptr);
    dataList.emplace_back(15, nullptr);
    dataList.emplace_back(16, nullptr);
    dataList.emplace_back(17, nullptr);
    dataList.emplace_back(18, nullptr);
    dataList.emplace_back(19, nullptr);

    //     nowPageLayout = nPageLayout;
    // dataList = data;
    // LOGE("111111111111111111111111111");
    // AnimationHandler::getInstance().setFrameDelay(200); // 动画帧延迟
    // LOGE("222222222222222222222222222");

    // // MyPageAdapter *testAdapter = new MyPageAdapter(wind_page_box);
    // LOGE("3333333333333333333333333");

    // LinearLayout *layout = new LinearLayout(336, 440);
    // layout->setOrientation(LinearLayout::VERTICAL);
    // LOGE("44444444444444444444444444444");

    // TabLayout *tab = new TabLayout(336, 180);
    // ViewPager *pager = new ViewPager(336, 440);
    // pager->setOffscreenPageLimit(this->getCount());
    // pager->setOverScrollMode(View::OVER_SCROLL_ALWAYS);
    // pager->setAdapter(this);
    // pager->setBackgroundColor(0xFFF3333);
    // tab->setSelectedTabIndicatorColor(0x8000FF00);
    // tab->setSelectedTabIndicatorHeight(4);
    // tab->setTabIndicatorGravity(Gravity::BOTTOM);//TOP/BOTTOM/CENTER_VERTICAL/FILL_VERTICAL
    // tab->setupWithViewPager(pager);
    // layout->addView(tab).setId(1);
    // layout->addView(pager).setId(10);
    // tab->setTabTextColors(0xFFFF0000,0xFF00FF00);
    // LOGE("55555555555555555555555555555555");

    // // wind_page_box->addView(layout);
    // // wind_page_box->requestLayout();
    // LOGE("6666666666666666666666666666666666666");

    // // delete this;
    // LOGE("7777777777777777777777777777777777");
    //
}

int MyPageAdapterUp::getCount()
{
    return dataList.size();
    // return 2;
}
bool MyPageAdapterUp::isViewFromObject(View *view, void *object)
{
    return view == object;
}
void *MyPageAdapterUp::instantiateItem(ViewGroup *container, int position)
{

    // #if ENABLE(DAYTIME_WIDGETS)
    //     SimpleMonthView *sm = new SimpleMonthView(1280, 560);
    //     sm->setMonthParams(23, Calendar::MAY + position, 2021, -1, 1, 31);
    //     container->addView(sm).setId(100 + position);
    // #else
    //     TextView *tv = new TextView("test" + std::to_string(position), 110, 110);
    //     container->addView(tv);
    //     return tv;
    // #endif

    // ViewGroup *lunBoLayout = (ViewGroup *)LayoutInflater::from(container->getContext())->inflate("@layout/page_lunbo", nullptr);
    // ViewGroup *lunBoLayout2 = (ViewGroup *)LayoutInflater::from(container->getContext())->inflate("@layout/page_lunbo2", nullptr);
    // container->addView(lunBoLayout);
    // return lunBoLayout;

    if (position == 0 || position == 2 || position == 4 || position == 6 || position == 8 || position == 10 || position == 12 || position == 14 || position == 16 || position == 18)
    {
        lunBoLayoutUp = (ViewGroup *)LayoutInflater::from(container->getContext())->inflate("@layout/page_lunbo_up", nullptr);
    }
    else if (position == 1 || position == 3 || position == 5 || position == 7 || position == 9 || position == 11 || position == 13 || position == 15 || position == 17 || position == 19)
    {
        lunBoLayoutUp = (ViewGroup *)LayoutInflater::from(container->getContext())->inflate("@layout/page_lunbo_up2", nullptr);
        temperatureNum = (TextView *)lunBoLayoutUp->findViewById(demo::R::id::temperature_num);
        date = (TextView *)lunBoLayoutUp->findViewById(demo::R::id::date);
        weather = (ImageView *)lunBoLayoutUp->findViewById(demo::R::id::weather);

        // myTickerForWeather = std::bind(&MyPageAdapterUp::onTickerForWeather, this);
        // myTickerForDay = std::bind(&MyPageAdapterUp::onTickerForDay, this);
        weather->setImageResource(g_appData.weather);
        temperatureNum->setText(std::to_string(g_appData.temperature));
        date->setText(getMonthDayDateTime() + " " + getWeekDay());
    }

    dataList[position].adapterLayout = lunBoLayoutUp;
    container->addView(lunBoLayoutUp);
    return lunBoLayoutUp;
}
void MyPageAdapterUp::destroyItem(ViewGroup *container, int position, void *object)
{
    LOGE("****************************** position = %d", position);
    container->removeView((View *)object);
    delete (View *)object;
}
std::string MyPageAdapterUp::getPageTitle(int position)
{
    // LOGE("----------------getPageTitle start------------------");
    // return std::to_string(dataList[position].pos);
    // 正常用法
    // dataList.at(position).name= std::string("tab") + std::to_string(position);
    // return dataList.at(position).name;
    // 不需要文本
    return "";
}
float MyPageAdapterUp::getPageWidth(int position)
{
    return 1.f;
}

void MyPageAdapterUp::updateItem(int position)
{
    // weather->setImageResource(g_appData.weather);
    // temperatureNum->setText(std::to_string(g_appData.temperature));
    // date->setText(getMonthDayDateTime() + " " + getWeekDay());
    // notifyDataSetChanged();
}

int MyPageAdapterUp::getItemPosition(void *object)
{
    return PagerAdapter::POSITION_NONE;
}